<?php
namespace Common\Model;

use Think\Model;

class DealerUserinfoModel extends Model
{
    protected $_validate = [
        ['open_id', '', '微信ID已注册！', 0, 'unique', 1],
        ['company_name', 'require', '请填写公司名'],
        ['username', 'require', '请填写姓名'],
        ['username', '', '此用户名已经存在！', 0, 'unique', 1],
        ['password', 'require', '请输入密码！'],
        ['password', '6,18', '密码长度错误!', 0, 'length'],
        ['mobile_phone', 'require', '请填写手机号！'],
        ['company_address', 'require', '请填写公司地址填写！'],
        ['belong_to_area', 'require', '请选择所在区域！']
    ];

    //绑定注册用户
    public function bindingRegister()
    {
        if (!$this->create()) {
            $arr['state'] = 'error';
            $arr['info'] = $this->getError();
            return $arr;
        } else {
            $_POST['sr_time'] = time();
            $_POST['password'] = md5($_POST['password']);
            if ($userId = $this->add($_POST)) {
                cookie('wap_openid', $_POST['openid'], array('expire' => 3600 * 3600 * 24));
                cookie('wap_userId', $userId, array('expire' => 3600 * 3600 * 24));
                $arr['state'] = 'success';
                $arr['info'] = '注册成功';
            } else {
                $arr['state'] = 'error';
                $arr['info'] = '注册失败';
            }
            return $arr;
        }
    }

    //用户登录
    public function userLogin()
    {
        if ($_POST['mobile_phone'] == '') {
            $arr['state'] = 'error';
            $arr['info'] = '请填写用户名';
            return $arr;
        }
        if ($_POST['password'] == '') {
            $arr['state'] = 'error';
            $arr['info'] = '请填写登录密码';
            return $arr;
        }
        $mobile_phone = $_POST['mobile_phone'];
        $password = md5($_POST['password']);
        $login_result = $this->where("mobile_phone='%s' and password='%s'", [$mobile_phone, $password])->find();
        if ($login_result) {
            if ($login_result['status'] == 1) {
                cookie('wap_openid', $login_result['open_id'], array('expire' => 3600 * 3600 * 24));
                cookie('wap_userId', $login_result['id'], array('expire' => 3600 * 3600 * 24));
                $arr['state'] = 'success';
                $arr['info'] = '登录成功';
            } else {
                $arr['state'] = 'error';
                $arr['info'] = '登录失败,账号被禁用';
            }
            return $arr;
        } else {
            $arr['state'] = 'error';
            $arr['info'] = '用户名或密码错误';
        }
        return $arr;
    }

    //用户列表
    public function userList($id = '',$where='')
    {
        if (empty($id)) {
            $counts = $this->where($where)->count();
            $page = page($counts, 20, 5);
            $list = $this->field("a.*,b.area_name")->alias("a")->join("sr_area as b on a.belong_to_area=b.id")
                ->order("id desc")->where($where)->limit($page->firstRow, $page->listRows)->select();
            $arr['page'] = $page;
            $arr['list'] = $list;
            return $arr;
        }

    }
    public function userDetail($id)
    {

       return $this->where("id=%d",$id)->find();
    }




}





















